package leetcode;

import java.util.Arrays;
import java.util.Comparator;

public class Q0452 {
    public int findMinArrowShots(int[][] points) {
        Arrays.sort(points, Comparator.comparingLong(a -> a[1]));
        int l = points[0][0];
        int r = points[0][1];
        int res = 1;

        for (int i = 1; i < points.length; i++) {
            if (points[i][0] > r) {
                res++;
                l = points[i][0];
                r = points[i][1];
            } else {
                l = Math.max(l, points[i][0]);
                r = Math.min(r, points[i][1]);
            }
        }

        return res;
    }

    public static void main(String[] args) {
        Q0452 sl = new Q0452();
        int[][] points = {{9, 12}, {1, 10}, {4, 11}, {8, 12}, {3, 9}, {6, 9}, {6, 7}};
        System.out.println(sl.findMinArrowShots(points));
    }
}
